package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.MyCareMeAdapter;
import com.dingwei.superiordiary.adapter.adapter.MyCarePersonalAdapter;
import com.dingwei.superiordiary.bean.MyCareMeBean;
import com.dingwei.superiordiary.bean.MyCarePersonalBean;
import com.dingwei.superiordiary.mvp.presenter.MyCareMePresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/11.
 */

public class MyCareMeActivity extends BaseActivity implements Contract.MyCareMeView {

    MyCareMePresenter myCareMePresenter = new MyCareMePresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;


    private MyCareMeAdapter adapter;
    //    private RecyclerTouchListener onTouchListener;
    private int page = 1;
    private String userid;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_collect;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    protected void initView() {
        userid = getIntent().getStringExtra("userid");
        if (BaseActivity.value.getUserId().equals(userid)){
            tvTitle.setText("关注我的人");
        }else {

            tvTitle.setText("关注TA的人");
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.parting_line), DisplayUtil.dip2px(BaseActivity.context, 1)));
        adapter = new MyCareMeAdapter(this);
        mRecyclerView.setAdapter(adapter);

    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                myCareMePresenter.initData(page, userid,false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                myCareMePresenter.initData(page, userid,true, false);
            }
        });

        adapter.attentionInterfaceListener(new MyCarePersonalAdapter.attentionInterFace() {
            @Override
            public void attention(int position) {
                MyCareMeBean.DataBean.ListBean data = adapter.getItem(position);
                int action = 0;
                if (data.attentioned.equals("0")) {
                    action = 1;
                } else {
                    action = 0;
                }
                myCareMePresenter.attention(data.id, position, action);
            }
        });
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int i) {
                MyCareMeBean.DataBean.ListBean data = adapter.getItem(i);
                intent = new Intent(BaseActivity.context,LookUserCenterActivity.class);
                intent.putExtra("user_id",data.id);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
//        mRecyclerView.addOnItemTouchListener(onTouchListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        mRecyclerView.removeOnItemTouchListener(onTouchListener);
    }

    public void showData() {
        if (adapter.getAllData() == null || adapter.getAllData().size() == 0) {
            noDataLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
            noDataLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh(MyCareMeBean bean) {
        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData();
    }

    @Override
    public void onLoadMore(MyCareMeBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void onFail() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void attentionSuccess(int position) {
        MyCareMeBean.DataBean.ListBean listBean = adapter.getItem(position);
        if (listBean.attentioned.equals("0")) {
            listBean.attentioned = "1";
        } else {
            listBean.attentioned = "0";
        }
        adapter.notifyItemChanged(position, listBean);
    }


    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
